#include <QApplication>
#include <opencv2/opencv.hpp>
#include <palette/WWClassicVGAPalette.h>
#include <converter/FrameConverter.h>
#include <shp/Shp.h>
#include <PaletteShower.h>
#include <IndexedShapeShower.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto pReader = WWClassicVGAPaletteReader();
    auto pal = pReader.ReadFormFile(R"(C:\Users\Castle\Desktop\unittem.pal)");
    auto cdc = std::make_shared<EulerColorDiffCalc>();
    auto converter = FrameConverter(pal, cdc);

    auto img = cv::imread(R"(C:\Users\Castle\Desktop\ark_icon.png)", cv::IMREAD_UNCHANGED);
    cvtColor(img, img, cv::COLOR_BGRA2RGBA);
    auto data = img.data;
    auto w = img.cols;
    auto h = img.rows;

    auto out = Uint8Seq(w * h);
    converter.Convert(data, w * h, FrameConverter::ImageDataType::RGBA, out);

    auto frameCount = 1;

    auto shp = make_shared<Shp>();
    shp->header.height = h;
    shp->header.width = w;
    shp->header.frameTotal = frameCount;

    shp->frames.reserve(frameCount);

    shp->frames.emplace_back(Shp::ShpFrame());
    shp->frames[0].width = w;
    shp->frames[0].height = h;
    shp->frames[0].x = 0;
    shp->frames[0].y = 0;
    shp->frames[0].data = make_shared<Uint8Seq>(w * h);
    shp->frames[0].data->swap(out);

    shp->Write(R"(C:\Users\Castle\Desktop\c.shp)");

    auto w1 = new PaletteShower();
    w1->SetPalette(pal);
    w1->show();

    auto w2 = new IndexedShapeShower(pal);
    w2->SetShp(shp);
    w2->show();

    return QApplication::exec();
}
